package cn.fan.springboot_easyexcle.control;

import cn.fan.springboot_easyexcle.dao.easyreadDao;
import cn.fan.springboot_easyexcle.model.DriverbyVehicleExportErrorVo;
import cn.fan.springboot_easyexcle.model.JsonMessage;
import cn.fan.springboot_easyexcle.model.UserExportVO;
import cn.fan.springboot_easyexcle.service.UserService;
import cn.fan.springboot_easyexcle.util.EasyExcelReadUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;

/**
 * * code is far away from bug with the animal protecting
 * ┏┓　 ┏┓
 * ┏┛┻━━━┛┻┓
 * ┃　　　　　　┃
 * ┃　　　━　　┃
 * ┃　┳┛　┗┳　┃
 * ┃　　　　　　┃
 * ┃　　　┻　　┃
 * ┃　　　　　　┃
 * ┗━┓　　　┏━┛
 * 　　┃　　　┃神兽保佑
 * 　　┃　　　┃代码无BUG！
 * 　　┃　　　┗━━━┓
 * 　　┃　　　　　 ┣┓
 * 　　┃　　　　　 ┏┛
 * 　　┗┓┓ ┏━┳┓ ┏┛
 * 　　　┃┫┫　┃┫┫
 * 　　　┗┻┛　┗┻┛
 *
 * @description:
 * @author: fgy
 * @create: 2023-04-11 15:37
 **/
@Controller
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("export4")
    public void export4(HttpServletResponse response) throws IOException {
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String filenames = "bigdata4";
        response.addHeader("Content-Disposition", "filename=" + filenames + ".xlsx");
        ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream(), UserExportVO.class).build();
        WriteSheet[] writeSheet = new WriteSheet[]{
                EasyExcel.writerSheet(0, "sheet").build()
        };

        userService.export2(s -> {
            UserExportVO resultObject = s.getResultObject();
            ArrayList arrayList = new ArrayList<UserExportVO>();
            arrayList.add(resultObject);
            excelWriter.write(arrayList, writeSheet[0]);
        });

        excelWriter.finish();

    }

    /**
     * 基于EasyExcel的读取exl并返回成功/失败数量及原因,并将导入失败exl的导出到系统路径中
     *
     * @param response
     * @throws IOException
     */
    @RequestMapping("importExcle")
    public void importExcle(HttpServletResponse response, @RequestParam("file")
            MultipartFile file) throws IOException {
//        JsonMessage jsonMessage= EasyExcelReadUtil.easyRead(new easyreadDao(),"C:\\testss.xls", DriverbyVehicleExportErrorVo.class,"D:\\test\\err");
        JsonMessage jsonMessage = EasyExcelReadUtil.easyRead(new easyreadDao(), file.getInputStream(), DriverbyVehicleExportErrorVo.class, "D:\\test\\err");
        System.out.println(JSON.toJSONString(jsonMessage));

    }

    /**
     * 基于EasyExcel的读取exl并返回成功/失败数量及原因,并将导入失败excle直接导出
     *
     * @param response
     * @throws IOException
     */
    @RequestMapping("importExcle1")
    public void importExcle1(HttpServletResponse response, @RequestParam("file")
            MultipartFile file) throws IOException {
//        JsonMessage jsonMessage= EasyExcelReadUtil.easyRead(new easyreadDao(),"C:\\testss.xls", DriverbyVehicleExportErrorVo.class,"D:\\test\\err");
        JsonMessage jsonMessage = EasyExcelReadUtil.easyRead(new easyreadDao(), file.getInputStream(), DriverbyVehicleExportErrorVo.class, response);
        System.out.println(JSON.toJSONString(jsonMessage));
//        return jsonMessage;
    }
}

